// ====================Window_CardReward====================
class Window_CardReward extends CardContainerBase {

    initialize() {
        this._data = null;
        this.create();
        this.hide();
    }

    create() {
        this.createBackground();
        this.createContentSprite();
        this.createButtonSprite();
    }

    createBackground() {
        const {width, height} = this._opts;
        const bitmap = new Bitmap(width, height);
        const sprite = new Sprite(bitmap);
        bitmap.fontFace = $gameSystem.mainFontFace();
        bitmap.fontSize = 32;
        bitmap.fillRoundRect(0, 0, width, height, "#FFFFFF", 10);
        bitmap.fillRoundRect(3, 3, width - 6, height - 6, "#737373", 7);
        bitmap.drawText("奖励", 0, 10, width, 40, "center");
        this._background = sprite;
        this.addChild(sprite);
    }

    createContentSprite() {
        const {width, height} = this._opts;
        const bitmap = new Bitmap(width, height);
        const sprite = new Sprite(bitmap);
        bitmap.fontFace = $gameSystem.mainFontFace();
        this._contentSprite = sprite;
        this.addChild(sprite);
    }

    createButtonSprite() {
        this.createOkButton();
    }

    createOkButton() {
        const {width, height} = this._opts;
        const x = (width - 200) / 2;
        const y = height - 70
        const text = "确定";
        const func = this.onOk.bind(this);
        const button = new Sprite_CardButton(x, y, text, func);
        this._okButton = button;
        this.addChild(button);
    }

    open(data) {
        if (!!data) {
            this._data = data;
        }
        this.refresh();
        this.show();
    }

    refresh() {
        if (!this._data) return;
        const data = this._data;
        const bitmap = this._contentSprite.bitmap;
        bitmap.clear();
        for (let i = 0; i < data.length; i++) {
            const rect = this.itemRect(i);
            const item = data[i];
            const {type} = item;
            bitmap.fillRoundRect(rect.x, rect.y, rect.width, rect.height, "#57c6fa", 3);
            if (type === "gold") {
                const {amount} = item;
                bitmap.drawText(" 金币", rect.x, rect.y, rect.width, rect.height, "left");
                bitmap.drawText(`x${amount} `, rect.x, rect.y, rect.width, rect.height, "right");
            } else if (type === "card") {
                const card = item.select;
                bitmap.drawText(" 自选卡牌", rect.x, rect.y, rect.width, rect.height, "left");
                if (!card) {
                    bitmap.drawText("未选择 ", rect.x, rect.y, rect.width, rect.height, "right");
                } else {
                    const name = card.name();
                    bitmap.drawText(`已选择：${name} `, rect.x, rect.y, rect.width, rect.height, "right");
                }

            }
        }
    }

    itemRect(index) {
        const {width} = this._opts;
        const x = 20;
        const y = 50 + (index) * (30 + 10);
        const iw = width - x * 2;
        const ih = 30;
        return new Rectangle(x, y, iw, ih);
    }

    onOk() {
        for (const item of this._data) {
            if (item.type === "gold") continue;
            if (item.type === "card") {
                if (!item.select) {
                    SoundManager.playCancel();
                    return;
                } else {
                    const id = item.select.id();
                    $gameParty._deckLists.push(id);
                }
            }
        }
        SceneManager._scene.popScene();
        CardGameManager.clear();
    }

    updateContainer() {
        this.updateClick();
    }

    updateClick() {
        const point = this.toLocal(new Point(TouchInput.x, TouchInput.y));
        if (TouchInput.isClicked()) {
            for (let i = 0; i < this._data.length; i++) {
                const item = this._data[i];
                if (item.type === "card" && this.itemRect(i).contains(point.x, point.y)) {
                    const text = "请选择一张牌加入你的牌库";
                    const list = item.cards.map(id => {
                        return new Game_Card(id)
                    });
                    const callback = (card) => {
                        item.select = card;
                    };
                    this.hide();
                    CardGameManager.displayRewardCard({text, list}, callback);
                }
            }
        }
    }
}